package cn.com.zsc.system;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.com.zsc.system.vo.UserVO;
import cn.com.zsc.util.Azdg;
import cn.com.zsc.util.ConstValue;

import com.jarp.db.DAO;

/**
 * 用户管理类
 * @author 杨景
 *
 */
public class UserManage {
	/**
	 * 获取对应条数的用户信息
	 * 
	 * @param start
	 * @param limit
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getUser(String start, String limit) {
		String sql = "select * from app_login order by UID desc limit "+start+","+limit;

		DAO dao = new DAO();
		
		String resultStr = ConstValue.EMPTY_RECORD_PAGEING;
		
		try {
			Vector<Hashtable<String, String>> v = dao.getDataSet(ConstValue.DBNAME, sql);

			if (v != null && v.size() > 0) {
				List<UserVO> userList = new ArrayList<UserVO>();
				
				for (int i = 0; i < v.size(); i++) {
					Hashtable<String, String> table = (Hashtable<String, String>) v.get(i);
					UserVO user = new UserVO();

					user.setUid(Integer.parseInt(table.get("UID")));
					user.setUsername(table.get("UNAME"));
					user.setPassword(Azdg.decrypt(table.get("UPWD")));
					user.setName(table.get("NAME"));

					if (table.get("RID") != null && !"".equals(table.get("RID"))) {
						user.setRid(Integer.parseInt(table.get("RID")));
					}

					user.setRemarked(table.get("REMARKED"));
					userList.add(user);
				}

				JSONArray json = JSONArray.fromObject(userList);
				
				String userStr = json.toString();

				sql = "select count(UID) from app_login";
				
				String total = dao.getDataValue(ConstValue.DBNAME, sql);
				
				resultStr = "{\"totalCount\":\"" + total + "\",\"results\":" + userStr + "}";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return resultStr;
	}
	
	/**
	 * 验证登陆用户名
	 * @param loginname
	 * @param usercode
	 * @return
	 * @throws ServletException
	 */
	public boolean isExistLoginName(String username, String uid) throws ServletException {
		String sql = "";
		
		// 修改流程
		if (!"".equals(uid)) {
			sql = "select count(UID) from app_login where UID<>'"
					+ uid + "' and UNAME='" + username + "'";
		// 新增流程
		} else {
			sql = "select count(UID) from app_login where UNAME='"
					+ username + "'";
		}
		
		DAO dao = new DAO();
		
		String num = dao.getDataValue(ConstValue.DBNAME, sql);
	
		if (Integer.parseInt(num) > 0) {
			return true;
		}
		
		return false;
	}

	/**
	 * 新增用户记录
	 * @param jso
	 * @return
	 */
	public String insertUser(String jso) {
		JSONObject jsobject = JSONObject.fromObject(jso);
	
		String username = jsobject.getString("username");
		String password = Azdg.encrypt(jsobject.getString("password"));
		String name = jsobject.getString("name");
		String remarked = jsobject.getString("remarked");
		
		String sql = "insert into app_login values(default,'" + username
				+ "','" + password + "','" + name + "','" + remarked + "')";
		
		DAO dao = new DAO();
		
		int num = 0;
		
		try {
			if (isExistLoginName(username, "")) {
				return "您输入的用户登录名称【" + name + "】已存在。";
			}
			
			num = dao.execCommand(ConstValue.DBNAME, sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (num > 0) {
			return "success";
		} else {
			return "数据库操作失败";
		}
	}

	/**
	 * 修改用户记录
	 * @param jso
	 * @return
	 */
	public String updateUser(String jso) {
		JSONObject jsobject = JSONObject.fromObject(jso);
		
		String uid = jsobject.getString("uid");
		String username = jsobject.getString("username");
		String password = Azdg.encrypt(jsobject.getString("password"));
		String name = jsobject.getString("name");
		String remarked = jsobject.getString("remarked");
		
		String sql = "update app_login set UNAME='" + username + "',UPWD='"
				+ password + "',NAME='" + name + "',REMARKED='" + remarked
				+ "' where UID = '" + uid + "'";
		
		DAO dao = new DAO();
		try {
			if (isExistLoginName(username, uid)) {
				return "您输入的用户登录名称【" + name + "】已存在。";
			}
			
			int num = dao.execCommand(ConstValue.DBNAME, sql);
			
			if (num > 0) {
				return "success";
			} else {
				return "数据库操作失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

	/**
	 * 删除用户记录
	 * @param jso
	 * @return
	 */
	public String deleteUser(String jso) {
		JSONObject jsobject = JSONObject.fromObject(jso);
		
		String uid = jsobject.getString("uid");
		
		String delRole = "delete from app_purview where uid ='" + uid + "'";
		String delUser = "delete from app_login where uid ='" + uid + "'";
		
		DAO dao = new DAO();
		
		try {
			ArrayList<String> al = new ArrayList<String>();
			al.add(delRole);
			al.add(delUser);
			
			boolean flag = dao.execCommand(ConstValue.DBNAME, al);
			
			if (flag) {
				return "success";
			} else {
				return "数据库操作失败";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}
}
